package com.sgcc.fj.pmos.e_fj_mall.adapter;

import static com.sgcc.fj.pmos.e_fj_mall.adapter.GoodsAdapter.getMaxMonth;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.sgcc.fj.pmos.common.bean.PackageBean;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.utils.BigDecimalUtils;
import com.sgcc.fj.pmos.common.utils.DateUtils;
import com.sgcc.fj.pmos.common.utils.EnumUtils;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.common.utils.GlideUtil;
import com.sgcc.fj.pmos.common.utils.TextUtil;

import java.math.BigDecimal;

public class AllGoodsAdapter extends BaseQuickAdapter<PackageBean, BaseViewHolder> {
    private Context mContext;

    public AllGoodsAdapter(Context context) {
        super(R.layout.item_goods1);
        mContext = context;
    }

    public interface OnItemClickListener {
        //加入收藏
        void OnAddCollectionListener(int position, PackageBean s);

        //取消收藏
        void OnCancleCollectionListener(int position, PackageBean s);

        //条目点击
        void OnItemsClickListener(int position, PackageBean s);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, PackageBean bean) {
        String img_url = HttpInterConst.BASEURL_IMG + bean.getFileId();
        ImageView imageView = baseViewHolder.getView(R.id.iv_goods);
        GlideUtil.loadImg(img_url, imageView,R.mipmap.deft_goods);
//        Glide.with(mContext).load(img_url).placeholder(R.mipmap.deft_goods).error(R.mipmap.deft_goods).into(imageView);


//        baseViewHolder.setText(R.id.tv_price, BigDecimalUtils.onKeep2Decimal(bean.getPrice()));
        baseViewHolder.setText(R.id.tv_price, bean.getPrice());

        if ("1".equals(bean.getpNum()) && ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) &&
                "0".equals(bean.getMainItemType()) &&
                ("0".equals(bean.getMainDifType()) || TextUtils.isEmpty(bean.getMainDifType()))
        ) {//单一套餐&&不分时段&&固定价格&&没有浮动价差
            //蓝色
            baseViewHolder.setTextColor(R.id.tv_price, Color.parseColor("#0092FF"));
            baseViewHolder.setGone(R.id.tv_ygjg,true);
//            baseViewHolder.setGone(R.id.tv_lishiyugudianjia, true);
        } else {
            //橙色
            baseViewHolder.setTextColor(R.id.tv_price, Color.parseColor("#FFA500"));
            baseViewHolder.setGone(R.id.tv_ygjg,false);
//            baseViewHolder.setGone(R.id.tv_lishiyugudianjia, false);
        }

//        baseViewHolder.setText(R.id.tv_combName, bean.getSellingPoint());
        baseViewHolder.setText(R.id.tv_combName, TextUtil.getName(bean.getSellingPoint(),bean.getCreditRating()));
        baseViewHolder.setText(R.id.tv_membersSdName, bean.getCombCode());
        baseViewHolder.setText(R.id.tv_soldEnergy, TextUtils.isEmpty(bean.getSoldEnergy()) ? "0MWh" : "0.00000".equals(bean.getSoldEnergy()) ? "0MWh" : new BigDecimal(bean.getSoldEnergy()).stripTrailingZeros().toPlainString() + "MWh");
        baseViewHolder.setText(R.id.tv_time, DateUtils.getStrng_to_Times_ymd_1(bean.getEffectiveDate()) + "~" + DateUtils.getStrng_to_Times_ymd_1(bean.getInvalidDate()));
        String str = "";
        int maxMonth = getMaxMonth(bean);
        if ("04".equals(bean.getConstrMinCyc())) {
            str = maxMonth + "~" + maxMonth + "自然月";
        } else {
            if (TextUtils.isEmpty(bean.getConstrMin())) {
                if (TextUtils.isEmpty(bean.getConstrMax())) {
                    str = maxMonth + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                } else {
                    str = bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                }
            } else {
                if (TextUtils.isEmpty(bean.getConstrMax())) {
                    str = bean.getConstrMin() + "~" + maxMonth + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                } else {
                    str = bean.getConstrMin() + "~" +bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
                }
//                str = bean.getConstrMin() + "~" + bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
            }
        }
//        if (TextUtils.isEmpty(bean.getConstrMin())) {
//            str = bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
//        } else {
//            str = bean.getConstrMin() + "~" + bean.getConstrMax() + EnumUtils.getConstrMin(bean.getConstrMinCyc());
//        }
        if(bean.getConstrMinCyc().equals("04")){
            if(maxMonth==0){
                baseViewHolder.setText(R.id.tv_constrMinCyc,  "全部可购买月份");
            }else{
                baseViewHolder.setText(R.id.tv_constrMinCyc,  str);
            }
        }else{
            baseViewHolder.setText(R.id.tv_constrMinCyc,  str);
        }


//        baseViewHolder.setText(R.id.tv_constrMinCyc, bean.getConstrMinCyc().equals("04") ? "全部可购买月份" : str);
        baseViewHolder.setText(R.id.tv_min_max, bean.getStock());


        ///----------------
        baseViewHolder.setGone(R.id.ll_schemeRatio1, false);
        baseViewHolder.setGone(R.id.ll_schemeRatio2, false);
        baseViewHolder.setGone(R.id.rl_floating_content, false);
        baseViewHolder.setGone(R.id.tv_floating_package, false);
        baseViewHolder.setGone(R.id.tv_floating_period, false);
        baseViewHolder.setGone(R.id.ll_type, false);

        if ("2".equals(bean.getpNum())) {//单一套餐：1                 组合套餐：2
            //显示   组合套餐
            baseViewHolder.setGone(R.id.ll_schemeRatio1, false);
            baseViewHolder.setGone(R.id.ll_schemeRatio2, false);
            baseViewHolder.setTextColor(R.id.tv_schemeRatio1, Color.parseColor("#181818"));
            baseViewHolder.setTextColor(R.id.tv_schemeRatio2, Color.parseColor("#181818"));
//            if (!TextUtils.isEmpty(bean.getMainPrice()) && !TextUtils.isEmpty(bean.getFloPrice())) {
//                if (BigDecimalUtils.compareTo(new BigDecimal(bean.getMainPrice()), new BigDecimal(bean.getFloPrice())) == -1) {
//                    baseViewHolder.setTextColor(R.id.tv_schemeRatio1, Color.parseColor("#FFA500"));
//                }
//                if (BigDecimalUtils.compareTo(new BigDecimal(bean.getMainPrice()), new BigDecimal(bean.getFloPrice())) == 1) {
//                    baseViewHolder.setTextColor(R.id.tv_schemeRatio2, Color.parseColor("#FFA500"));
//                }
//            }
            baseViewHolder.setText(R.id.tv_schemeRatio1, "K1=" +bean.getSchemeRatio1() + "%");
            baseViewHolder.setText(R.id.tv_schemeRatio2, "K2=" +bean.getSchemeRatio2() + "%");
            baseViewHolder.setText(R.id.tv_base_package, "基准套餐");

            String mainType = TextUtils.isEmpty(bean.getMainType()) ? "" : bean.getMainType();
            switch (mainType) {//基准   分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
                case "01":
                    baseViewHolder.setText(R.id.tv_base_period, "分时段 约定电量");
                    break;
                case "02":
                    baseViewHolder.setText(R.id.tv_base_period, "分时段 不约定电量");
                    break;
                case "03":
                    baseViewHolder.setText(R.id.tv_base_period, "不分时段 约定电量");
                    break;
                case "04":
                    baseViewHolder.setText(R.id.tv_base_period, "不分时段 不约定电量");
                    break;
            }

            String floType = TextUtils.isEmpty(bean.getFloType()) ? "" : bean.getFloType();
            switch (floType) {//浮动   分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
                case "01":
                    baseViewHolder.setText(R.id.tv_floating_period, "分时段 约定电量");
                    break;
                case "02":
                    baseViewHolder.setText(R.id.tv_floating_period, "分时段 不约定电量");
                    break;
                case "03":
                    baseViewHolder.setText(R.id.tv_floating_period, "不分时段 约定电量");
                    break;
                case "04":
                    baseViewHolder.setText(R.id.tv_floating_period, "不分时段 不约定电量");
                    break;
            }

            //基准
            if ("1".equals(bean.getMainItemType())) {//1显示汉字，0显示数字
                baseViewHolder.setText(R.id.tv_base_left, bean.getMainItemName());
                //橙色
                baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#FFA500"));
            } else {
//                baseViewHolder.setText(R.id.tv_base_left, BigDecimalUtils.onKeep2Decimal(bean.getMainItemValue()));
                baseViewHolder.setText(R.id.tv_base_left, bean.getMainItemValue());
                if ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) {
                    //蓝色
                    baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#0092FF"));
                } else {
                    //橙色
                    baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#FFA500"));
                }
            }

            baseViewHolder.setGone(R.id.tv_base_center0, false);
            baseViewHolder.setGone(R.id.tv_base_right2, false);
            if ("1".equals(bean.getMainDifType())) {//1显示汉字，0显示数字
                baseViewHolder.setGone(R.id.tv_base_center2, false);
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setText(R.id.tv_base_center2, "+浮动价差");
            } else if ("0".equals(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_center1, false);
                baseViewHolder.setGone(R.id.tv_base_center2, true);
                baseViewHolder.setText(R.id.tv_base_center1, (bean.getMainDifValue().contains("-") ? bean.getMainDifValue() : "+" + bean.getMainDifValue()));

//                baseViewHolder.setText(R.id.tv_base_center1, "+" + BigDecimalUtils.onKeep2Decimal(bean.getMainDifValue()));
            } else if ("01".equals(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setGone(R.id.tv_base_center2, false);
//                baseViewHolder.setText(R.id.tv_base_center1, "+" + BigDecimalUtils.onKeep2Decimal(bean.getMainDifValue()));
                baseViewHolder.setText(R.id.tv_base_center2, "+浮动价差");
            } else {
                baseViewHolder.setGone(R.id.tv_base_center0, true);
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setGone(R.id.tv_base_center2, true);
                baseViewHolder.setGone(R.id.tv_base_right2, true);
            }

            if ("0".equals(bean.getMainItemType()) && "0".equals(bean.getMainDifType())) {
                if ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) {
                    baseViewHolder.setText(R.id.tv_base_right2, ")=");
                    baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#0092FF"));
                } else {
                    baseViewHolder.setText(R.id.tv_base_right2, ")≈");
                    baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#FFA500"));
                }
            } else if ("1".equals(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_right2, false);
                baseViewHolder.setText(R.id.tv_base_right2, "≈");
            } else {
                baseViewHolder.setText(R.id.tv_base_right2, ")≈");
                baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#FFA500"));
            }

            if ("0".equals(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setText(R.id.tv_base_right1, "");
            } else if (TextUtils.isEmpty(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setText(R.id.tv_base_left, "");
//                baseViewHolder.setText(R.id.tv_base_right1, BigDecimalUtils.onKeep2Decimal(bean.getMainPrice()));
                baseViewHolder.setText(R.id.tv_base_right1, bean.getMainPrice());
            } else {
//                baseViewHolder.setText(R.id.tv_base_right1, BigDecimalUtils.onKeep2Decimal(bean.getMainPrice()));
                baseViewHolder.setText(R.id.tv_base_right1, bean.getMainPrice());
            }


            //浮动
            if ("1".equals(bean.getFloItemType())) {//1显示汉字，0显示数字
                baseViewHolder.setText(R.id.tv_floating_left, bean.getFloItemName());
                //橙色
                baseViewHolder.setTextColor(R.id.tv_floating_left, Color.parseColor("#FFA500"));
            } else {
                baseViewHolder.setText(R.id.tv_floating_left, bean.getFloItemValue());
                if ("03".equals(bean.getFloType()) || "04".equals(bean.getFloType())) {
                    //蓝色
                    baseViewHolder.setTextColor(R.id.tv_floating_left, Color.parseColor("#0092FF"));
                } else {
                    //橙色
                    baseViewHolder.setTextColor(R.id.tv_floating_left, Color.parseColor("#FFA500"));
                }
            }

            baseViewHolder.setGone(R.id.tv_floating_center0, false);
            baseViewHolder.setGone(R.id.tv_floating_right2, false);
            if ("1".equals(bean.getFloDifType())) {//1显示汉字，0显示数字
                baseViewHolder.setGone(R.id.tv_floating_center1, true);
                baseViewHolder.setGone(R.id.tv_floating_center2, false);
                baseViewHolder.setText(R.id.tv_floating_center2, "+浮动价差");
            } else if ("0".equals(bean.getFloDifType())) {
                baseViewHolder.setGone(R.id.tv_floating_center1, false);
                baseViewHolder.setGone(R.id.tv_floating_center2, true);
                baseViewHolder.setText(R.id.tv_floating_center1, (bean.getFloDifValue().contains("-") ? bean.getFloDifValue() : "+" + bean.getFloDifValue()));

//                baseViewHolder.setText(R.id.tv_floating_center1, "+" + bean.getFloDifValue());
            } else if ("01".equals(bean.getFloDifType())) {
                baseViewHolder.setGone(R.id.tv_floating_center1, true);
                baseViewHolder.setGone(R.id.tv_floating_center2, false);
//                baseViewHolder.setText(R.id.tv_floating_center1, "+" + BigDecimalUtils.onKeep2Decimal(bean.getFloDifValue()));
                baseViewHolder.setText(R.id.tv_floating_center2, "+浮动价差");
            } else {
                baseViewHolder.setGone(R.id.tv_floating_center0, true);
                baseViewHolder.setGone(R.id.tv_floating_center1, true);
                baseViewHolder.setGone(R.id.tv_floating_center2, true);
                baseViewHolder.setGone(R.id.tv_floating_right2, true);
            }


            if ("0".equals(bean.getFloItemType()) && "0".equals(bean.getFloDifType())) {
                if ("03".equals(bean.getFloType()) || "04".equals(bean.getFloType())) {
                    baseViewHolder.setText(R.id.tv_floating_right2, ")=");
                    baseViewHolder.setTextColor(R.id.tv_floating_right1, Color.parseColor("#0092FF"));
                } else {
                    baseViewHolder.setText(R.id.tv_floating_right2, ")≈");
                    baseViewHolder.setTextColor(R.id.tv_floating_right1, Color.parseColor("#FFA500"));
                }
            } else if ("1".equals(bean.getFloItemType()) && TextUtils.isEmpty(bean.getFloDifType())) {
                baseViewHolder.setGone(R.id.tv_floating_right2, false);
                baseViewHolder.setText(R.id.tv_floating_right2, "≈");
                baseViewHolder.setTextColor(R.id.tv_floating_right1, Color.parseColor("#FFA500"));
            } else {
                baseViewHolder.setText(R.id.tv_floating_right2, ")≈");
                baseViewHolder.setTextColor(R.id.tv_floating_right1, Color.parseColor("#FFA500"));
            }

            if ("0".equals(bean.getFloItemType()) && TextUtils.isEmpty(bean.getFloDifType())) {
                baseViewHolder.setText(R.id.tv_floating_right1, "");
            } else if (TextUtils.isEmpty(bean.getFloItemType()) && TextUtils.isEmpty(bean.getFloDifType())) {
                baseViewHolder.setText(R.id.tv_floating_left, "");
                baseViewHolder.setText(R.id.tv_floating_right1, bean.getFloPrice());
            } else {
                baseViewHolder.setText(R.id.tv_floating_right1, bean.getFloPrice());
            }


        } else {
            //隐藏
            baseViewHolder.setGone(R.id.ll_schemeRatio1, true);
            baseViewHolder.setGone(R.id.ll_schemeRatio2, true);
            baseViewHolder.setGone(R.id.rl_floating_content, true);
            baseViewHolder.setText(R.id.tv_base_package, "基础价格套餐");
            baseViewHolder.setGone(R.id.tv_floating_package, true);
            baseViewHolder.setGone(R.id.tv_floating_period, true);
            baseViewHolder.setGone(R.id.ll_type, true);

            String mainType = TextUtils.isEmpty(bean.getMainType()) ? "" : bean.getMainType();
            switch (mainType) {//基准   分时分电量01，分时不分电量02，不分时分电量03，不分时不分电量04
                case "01":
                    baseViewHolder.setText(R.id.tv_base_period, "分时段 约定电量");
                    break;
                case "02":
                    baseViewHolder.setText(R.id.tv_base_period, "分时段 不约定电量");
                    break;
                case "03":
                    baseViewHolder.setText(R.id.tv_base_period, "不分时段 约定电量");
                    break;
                case "04":
                    baseViewHolder.setText(R.id.tv_base_period, "不分时段 不约定电量");
                    break;
            }


            //单一套餐
            if ("1".equals(bean.getMainItemType())) {//1显示汉字，0显示数字
                baseViewHolder.setText(R.id.tv_base_left, bean.getMainItemName());
                //橙色
                baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#FFA500"));
            } else {
//                baseViewHolder.setText(R.id.tv_base_left, BigDecimalUtils.onKeep2Decimal(bean.getMainItemValue()));
                baseViewHolder.setText(R.id.tv_base_left, bean.getMainItemValue());
                if ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) {
                    //蓝色
                    baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#0092FF"));
                } else {
                    //橙色
                    baseViewHolder.setTextColor(R.id.tv_base_left, Color.parseColor("#FFA500"));
                }
            }

            baseViewHolder.setGone(R.id.tv_base_center0, false);
            baseViewHolder.setGone(R.id.tv_base_right2, false);
            if ("1".equals(bean.getMainDifType())) {//1显示汉字，0显示数字
                baseViewHolder.setGone(R.id.tv_base_center2, false);
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setText(R.id.tv_base_center2, "+浮动价差");
            } else if ("0".equals(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_center1, false);
                baseViewHolder.setGone(R.id.tv_base_center2, true);
//                baseViewHolder.setText(R.id.tv_base_center1, "+" + BigDecimalUtils.onKeep2Decimal(bean.getMainDifValue()));
                baseViewHolder.setText(R.id.tv_base_center1, (bean.getMainDifValue().contains("-") ? bean.getMainDifValue() : "+" + bean.getMainDifValue()));

            } else if ("01".equals(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setGone(R.id.tv_base_center2, false);
//                baseViewHolder.setText(R.id.tv_base_center1, "+" + BigDecimalUtils.onKeep2Decimal(bean.getMainDifValue()));
                baseViewHolder.setText(R.id.tv_base_center2, "+浮动价差");
            } else {
                baseViewHolder.setGone(R.id.tv_base_center0, true);
                baseViewHolder.setGone(R.id.tv_base_center1, true);
                baseViewHolder.setGone(R.id.tv_base_center2, true);
                baseViewHolder.setGone(R.id.tv_base_right2, true);
            }

            if ("0".equals(bean.getMainItemType()) && "0".equals(bean.getMainDifType())) {
                if ("03".equals(bean.getMainType()) || "04".equals(bean.getMainType())) {
                    baseViewHolder.setText(R.id.tv_base_right2, ")=");
                    baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#0092FF"));
                } else {
                    baseViewHolder.setText(R.id.tv_base_right2, ")≈");
                    baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#FFA500"));
                }
            } else if ("1".equals(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setGone(R.id.tv_base_right2, false);
                baseViewHolder.setText(R.id.tv_base_right2, "≈");
            } else {
                baseViewHolder.setText(R.id.tv_base_right2, ")≈");
                baseViewHolder.setTextColor(R.id.tv_base_right1, Color.parseColor("#FFA500"));
            }

            if ("0".equals(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setText(R.id.tv_base_right1, "");
            } else if (TextUtils.isEmpty(bean.getMainItemType()) && TextUtils.isEmpty(bean.getMainDifType())) {
                baseViewHolder.setText(R.id.tv_base_left, "");
//                baseViewHolder.setText(R.id.tv_base_right1, BigDecimalUtils.onKeep2Decimal(bean.getMainPrice()));
                baseViewHolder.setText(R.id.tv_base_right1,bean.getMainPrice());
            } else {
//                baseViewHolder.setText(R.id.tv_base_right1, BigDecimalUtils.onKeep2Decimal(bean.getMainPrice()));
                baseViewHolder.setText(R.id.tv_base_right1, bean.getMainPrice());
            }

        }

        ///----------------

        baseViewHolder.setGone(R.id.rl_gongneng, true);
        baseViewHolder.setGone(R.id.view_bottom_btn_line, true);

        ///----------------

        baseViewHolder.setText(R.id.tv_green, EnumUtils.getGreenLeft(bean.getConstrRel()) + "  " + EnumUtils.getGreenCenter(bean.getConstrMod()) + "  " + EnumUtils.getGreenRight(bean.getConstrBarg()));

        baseViewHolder.getView(R.id.rl_store).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.OnItemsClickListener(getItemPosition(bean), bean);
                }
            }
        });
    }

}
